/*
 * @Author: 9ilk 3535851673@qq.com
 * @Date: 2025-01-19 14:30:51
 * @LastEditors: 9ilk 3535851673@qq.com
 * @LastEditTime: 2025-03-18 22:34:02
 * @FilePath: /zhuang/httpserver/Common.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

#pragma once
#include<iostream>
#include<string>
#include <fcntl.h>
#include <unistd.h>
#define Die(code) do{exit(code);}while(0)
#define CONV(v) ( struct sockaddr*)(v) //强转
using namespace std;

enum
{
  USAGE_ERR=1,
  SOCKET_ERR,
  BIND_ERR,
  LISTEN_ERROR,
  EPOLL_CREATE_ERR,
  EPOLL_CTL_ERR
};

//1.正常字符串 2.out字符串&&返回值是true 3.out空串&&返回值是false
 bool ParseOneLine(string& str,string* out,const string& Sep) //字符串解析/分割
 {
     auto pos = str.find(Sep);
     if(pos == string::npos) return false;
     *out = str.substr(0,pos); 
     str.erase(0,pos+Sep.size()); 
     return true;
 }

 bool SplitString(const string& header,const string& HeaderLineSep,string* key,string* value)
 { 
    auto pos = header.find(HeaderLineSep);
    if(pos == string::npos) return false;
    *key = header.substr(0,pos);
    *value = header.substr(pos+HeaderLineSep.size());
    return true;
 }

 void SetNonBlock(int sockfd) //设置为非阻塞
 {
   int fl = ::fcntl(sockfd,F_GETFL); //获取属性
   if(fl < 0)
    return;
   fcntl(sockfd,F_SETFL,fl|O_NONBLOCK);  //设置属性
 }